local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local gameController
local BALLON_FACTOR = 5
local levelAudio = audio.loadSound("levelSound.mp3")
local levelChannel

----------------------------------------------------------------------------------
-- 
--      NOTE:
--      
--      Code outside of listener functions (below) will only be executed once,
--      unless storyboard.removeScene() is called.
-- 
---------------------------------------------------------------------------------


-- local forward references should go here --


---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- Called when the scene's view does not exist:
function scene:createScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      CREATE display objects and add them to 'group' here.
        --      Example use-case: Restore 'group' from previously saved state.

        -----------------------------------------------------------------------------
		
		
		local function levelEndPopup(score)
			print("show end level popup score "..score)				
			
			if (global_levelNumber < LAST_LEVEL) then
				storyboard.gotoScene("EndLevelPopup",{time=500,effect="slideUp",params={totalScore=score}})
			else
				storyboard.gotoScene("GameEnd",{time=500,effect="fade",params={totalScore=score}})
			end
					
		
		end
		
		levelNumber = display.newText(global_levelNumber,50,25,"Nanum Pen Script",30)
		levelNumber:setTextColor(255,255,255)
		
		gameController = require("GameController")
		print("loading level " .. global_levelNumber)
		
		levelScoreValue = display.newText("0",50,90,"Nanum Pen Script",30)
		levelScoreValue:setTextColor(0,0,255)
		
		gameController.createLevelBallons(global_levelNumber*BALLON_FACTOR,group,levelEndPopup,levelScoreValue)
		
	
		upperBar = display.newImageRect("pauseBar-4G.png",249,43)
		
		upperBar.x = display.contentWidth / 2
		upperBar.y = 36
	
		pauseButtonUp = display.newImageRect("pauseBnt.png",64,64)
		pauseButtonUp.x = display.contentWidth - 35
		pauseButtonUp.y = 36

		pauseButtonDown = display.newImageRect("pauseBnt.png",64,64)
		pauseButtonDown.x = display.contentWidth - 35
		pauseButtonDown.y = 36
		pauseButtonDown:scale(1.1,1.1)
		pauseButtonDown.isVisible = false
	
		soundOnButtonUp = display.newImageRect("soundOnBnt.png",64,64)
		soundOnButtonUp.x = display.contentWidth - 90
		soundOnButtonUp.y = 36
		soundOnButtonUp:scale(0.5,0.5)

		soundOnButtonDown = display.newImageRect("soundOnBnt.png",64,64)
		soundOnButtonDown.x = display.contentWidth - 90
		soundOnButtonDown.y = 36
		soundOnButtonDown:scale(0.7,0.7)
		soundOnButtonDown.isVisible = false
		
		soundOff = display.newImageRect("off.png",13,13)
		soundOff.x = display.contentWidth - 90
		soundOff.y = 36
		soundOff.isVisible = false
		
		levelScoreBg = display.newImageRect("scoreHudBg.png",89,64)
		levelScoreBg.x = 55
		levelScoreBg.y = 98
		
		levelBg = display.newImageRect("levelHudBg.png",93,67)
		levelBg.x = 50
		levelBg.y = 35
		
		
		
		group:insert(upperBar)
		group:insert(pauseButtonUp)
		group:insert(pauseButtonDown)
		group:insert(levelScoreBg)
		group:insert(levelScoreValue)
		group:insert(levelBg)
		group:insert(levelNumber)
		group:insert(soundOnButtonUp)
		group:insert(soundOnButtonDown)
		group:insert(soundOff)
	
		local function pauseTouchListener(event) 
			if (event.phase == "began") then
				pauseButtonDown.isVisible = true
			else
				pauseButtonDown.isVisible = false				
				storyboard.showOverlay("Pause",{time=200,effect="fade",isModal=true,params={controler=gameController}})
				gameController.pauseGame()

			end	
			return true
		end 
		pauseButtonUp:addEventListener("touch", pauseTouchListener)
		
		local function soundTouchListener(event) 
			if (event.phase == "began") then
				soundOnButtonDown.isVisible = true
			elseif (event.phase == "ended") then
				soundOnButtonDown.isVisible = false				
				if (AUDIO_ENABLED == true) then
					audio.setVolume(0)
					AUDIO_ENABLED = false
					soundOff.isVisible = true
				else
					audio.setVolume(1)
					AUDIO_ENABLED = true
					soundOff.isVisible = false
				end
			end	
			return true
		end 
		soundOnButtonUp:addEventListener("touch", soundTouchListener)

end


-- Called BEFORE scene has moved onscreen:
function scene:willEnterScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end


-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
        local group = self.view
		levelChannel = audio.play(levelAudio,{loops = -1})
        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. start timers, load audio, start listeners, etc.)

        -----------------------------------------------------------------------------
		storyboard.purgeScene("Menu")
end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
        local group = self.view
        audio.stop(levelChannel)

        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

        -----------------------------------------------------------------------------

end


-- Called AFTER scene has finished moving offscreen:
function scene:didExitScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
        local group = self.view
		gameController.destroyGameController()
        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. remove listeners, widgets, save state, etc.)

        -----------------------------------------------------------------------------

end


-- Called if/when overlay scene is displayed via storyboard.showOverlay()
function scene:overlayBegan( event )
        local group = self.view
        local overlay_name = event.sceneName  -- name of the overlay scene

        -----------------------------------------------------------------------------

        --      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end


-- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()
function scene:overlayEnded( event )
        local group = self.view
        local overlay_name = event.sceneName  -- name of the overlay scene

        -----------------------------------------------------------------------------

        --      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end



---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "willEnterScene" event is dispatched before scene transition begins
scene:addEventListener( "willEnterScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "didExitScene" event is dispatched after scene has finished transitioning out
scene:addEventListener( "didExitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-- "overlayBegan" event is dispatched when an overlay scene is shown
scene:addEventListener( "overlayBegan", scene )

-- "overlayEnded" event is dispatched when an overlay scene is hidden/removed
scene:addEventListener( "overlayEnded", scene )

---------------------------------------------------------------------------------

return scene